﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace GameStateManagement
{
    public class OrbitingActor : Actor
    {
        protected Vector3 m_vAngleFromCenter;
        protected float m_fRadius;
        protected float m_fSpeed;
        protected Vector3 m_vRotationAxis;

        public OrbitingActor(Game game, Vector3 angleFromCenter, Vector3 rotationAxis, float radius, float speed)
            : base(game)
        {
            // Initialize data.
            m_vAngleFromCenter = angleFromCenter;
            m_fRadius = radius;
            m_fSpeed = speed;

            // Generate random rotation axis.
            Random rand = new Random((int)DateTime.Now.Ticks);
            m_vRotationAxis = new Vector3((float)rand.NextDouble(), (float)rand.NextDouble(), 0f);
            m_vRotationAxis = rotationAxis;
            m_vRotationAxis.Normalize();
        }

        // Randomized vectors
        public OrbitingActor(Game game)
            : base(game)
        {
            Random rand = new Random();

            // Initialize data.
            m_vAngleFromCenter = new Vector3();
            m_vAngleFromCenter.X = (float)Math.Cos(rand.NextDouble() * MathHelper.TwoPi * rand.Next(-2, 2));
            m_vAngleFromCenter.Y = (float)Math.Sin(rand.NextDouble() * MathHelper.TwoPi);
            m_vAngleFromCenter.Z = (float)Math.Tan(rand.NextDouble() * MathHelper.TwoPi);
            //m_vAngleFromCenter = new Vector3((float)rand.NextDouble() * 2f - 1f, (float)rand.NextDouble() * 2f - 1f, (float)rand.NextDouble() * 2f - 1f);
            m_vRotationAxis = new Vector3((float)rand.NextDouble() * 2f - 1f, (float)rand.NextDouble() * 2f - 1f, 0f);

            m_vAngleFromCenter.Normalize();
            m_vRotationAxis.Normalize();

            m_fRadius = 0f;
            m_fSpeed = 0f;
        }

        public override void Update(GameTime gameTime)
        {
            CalculatePosition();

            base.Update(gameTime);
        }

        private void CalculatePosition()
        {
            m_vAngleFromCenter = Vector3.Transform(m_vAngleFromCenter, Quaternion.CreateFromAxisAngle(m_vRotationAxis, m_fSpeed * GameplayScreen.fDelta));
            m_vWorldPosition = m_vAngleFromCenter * m_fRadius;
        }

        public void SetRadius(float radius)
        {
            m_fRadius = radius;
        }

        public void SetAngle(Vector3 newAngle)
        {
            m_vAngleFromCenter = newAngle;
        }

        public void ScaleRadiusBetweenSphereAndPlanet(float ratio)
        {
            m_fRadius = ((ShrinkingStarSphere.GetShrinkingStarSphere().GetRadius() - ShrinkingStarSphere.LevelPlanetRadius) * ratio) + ShrinkingStarSphere.LevelPlanetRadius;
        }
    }
}
